package com.shizhanzhe.zxbapp.vip;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.base.BaseFragment;
import com.shizhanzhe.zxbapp.bean.PDD;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.pay.PayVIP;
import com.shizhanzhe.zxbapp.pay.PayVIPActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.SortView;
import com.shizhanzhe.zxbapp.widget.VipDIalog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Created by zz9527 on 2018/6/6.
 */
public class VPFragment extends BaseFragment implements SortView.notifyOutsideListener {
    SortView sortView;

    Drawable[] sortDrawable;
    List<String> sortType = Arrays.asList("综合", "价格", "佣金比例");
    private String id;
    private ListView lv;

    public static VPFragment newInstance(String id) {

        Bundle args = new Bundle();
        args.putString("id", id);
        VPFragment fragment = new VPFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getString("id");
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_vp, container, false);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv = view.findViewById(R.id.lv);
        sortView = view.findViewById(R.id.sortView);
        sortDrawable = new Drawable[]{ContextCompat.getDrawable(getContext(), R.mipmap.icon_sort_defalut), ContextCompat.getDrawable(getContext(), R.mipmap.icon_sort_true), ContextCompat.getDrawable(getContext(), R.mipmap.icon_sort_false)};
        sortView.setType(sortType, sortDrawable);
        sortView.setNotifyOutsideListener(this);
        SmartRefreshLayout refresh = view.findViewById(R.id.refresh);
        refresh.setRefreshHeader(new ClassicsHeader(getActivity()));
        refresh.setRefreshFooter(new ClassicsFooter(getActivity()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                data.clear();
                getData();
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.PDD(id, type, page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        try {
                            Gson gson = new Gson();
                            List<PDD.DataBeanX.DataBean> list = gson.fromJson(json, PDD.class).getData().getData();
                            if (list.size() == 0) {
                                Toast.makeText(getContext(), "没有更多了", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            data.addAll(list);
                            adapter.notifyDataSetChanged();
                        } catch (Exception e) {
                            Toast.makeText(getContext(), "数据异常", Toast.LENGTH_SHORT).show();
                        }

                    }
                });
                refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
    }

    @Override
    protected void loadData() {
        getData();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext= activity;
    }
    private Context mContext;
    private int type = 1;
    private int page = 1;
    private String itemid = "";
    private int itemposition = -1;
    private PDDAdapter adapter;
    private List<PDD.DataBeanX.DataBean> data = new ArrayList<>();

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.PDD(id, type, page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                try {
//                    Log.i("___________",json);
                    Gson gson = new Gson();
                    data = gson.fromJson(json, PDD.class).getData().getData();
                    if (data.size() == 0) {
                        Toast.makeText(getContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    adapter = new PDDAdapter(data, getContext());
                    lv.setAdapter(adapter);
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            if (UserBean.level > 0) {
                                itemposition = i;
                                itemid = data.get(i).getGoods_id();
                                startActivityForResult(new Intent(getContext(), PDDDetailActivity.class).putExtra("goods_id", data.get(i).getGoods_id()), 120);
                            } else {
                                final VipDIalog vipDIalog = new VipDIalog(getContext());
                                vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                                    @Override
                                    public void chooseTrue() {
                                        getContext().startActivity(new Intent(getContext(), PayVIP.class));
                                        vipDIalog.dismiss();
                                    }

                                    @Override
                                    public void chooseFalse() {
                                        vipDIalog.dismiss();

                                    }
                                });
                                vipDIalog.show();
                                vipDIalog.setContent();
                            }
                        }
                    });
                } catch (Exception e) {
                    Toast.makeText(mContext, "数据异常", Toast.LENGTH_SHORT).show();
//                    Log.i("__________",e.toString());
}

            }
                    });
                    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent d) {
        super.onActivityResult(requestCode, resultCode, d);
        if (resultCode == 120) {
            data.remove(itemposition);
            adapter.notifyDataSetChanged();
            OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.PDDDELETE(itemid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                @Override
                public void onsendJson(String json) {
                }
            });
        }
    }

    @Override
    public void notifySort(String tag, int desOrasc) {
        page = 1;
        if (desOrasc == 0) {//由低到高
            if (tag.equals("价格")) {
                type = 2;
                getData();
            } else if (tag.equals("佣金比例")) {
                type = 4;
                getData();
            }
        } else {//由高到低
            if (tag.equals("综合")) {
                type = 1;
                getData();
            } else {
                if (tag.equals("价格")) {
                    type = 3;
                    getData();
                } else if (tag.equals("佣金比例")) {
                    type = 5;
                    getData();
                }
            }
        }
    }
}
